﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BE;
using BLL;
using VentaPorCatalogos.Models;

namespace VentaPorCatalogos
{
    /// <summary>
    /// Descripción breve de Imagen
    /// </summary>
    public class Imagen : IHttpHandler, System.Web.SessionState.IRequiresSessionState 
    {

        public void ProcessRequest(HttpContext context)
        {
            byte[] wImagen = null;
            if (HttpContext.Current.Request["Origen"] == "Articulo")
            {
                if (context.Session["Model"] != null)
                {
                    ArticulosModel oArticulosModel = context.Session["Model"] as ArticulosModel;
                    ArticuloBE oArticuloBE = oArticulosModel.Articulo;
                    wImagen = oArticuloBE.Imagen;
                }
            }
            else            
                if (HttpContext.Current.Request["IdArticulo"] != null)
                {
                    int wIdArticulo = int.Parse(HttpContext.Current.Request["IdArticulo"]);
                    ArticuloBE oArticuloBE = ArticuloBLL.SeleccionarPorId(wIdArticulo);
                    if (oArticuloBE != null)
                        wImagen = oArticuloBE.Imagen;
                }

            
            context.Response.ContentType = "image/jpg";

            if (wImagen != null)
                context.Response.OutputStream.Write(wImagen, 0, wImagen.Length);
            else
                context.Response.WriteFile("~/images/prohibido.jpeg");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}